package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.BaseEntity;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.UserDto;
import com.zzyl.entity.User;
import com.zzyl.entity.UserRole;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.UserService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {
    final static String  password = "888itcast.CN764%...";
    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleResourceMapper roleResourceMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResponse findByPage(UserDto userDto, int pageNum, int pageSize) {

        Set<Long> userIdSet= null;
        if (userDto.getRoleId()!= null) {
            userIdSet=userRoleMapper.selectUserByRoleId(userDto.getRoleId());
        }
        User bean = BeanUtil.toBean(userDto, User.class);
        PageHelper.startPage(pageNum, pageSize);
        Page<UserVo> page = userMapper.findByPage(bean,userIdSet);
        PageResponse pageResponse = PageResponse.of(page, UserVo.class);

        return pageResponse;
    }

    @Override
    public void save(UserDto userDto) {
        User bean = BeanUtil.toBean(userDto, User.class);

        bean.setDataState("0");
        bean.setUsername(bean.getEmail());
        bean.setPassword(BCrypt.hashpw(password,BCrypt.gensalt()));
        int insert = userMapper.insert(bean);
        if (insert == 0) {
            throw new RuntimeException("保存失败");
        }
        if (userDto.getRoleVoIds() != null && !userDto.getRoleVoIds().isEmpty()) {
            List<UserRole> list=new ArrayList<>();
            for (String roleVoId : userDto.getRoleVoIds()) {
                UserRole userRole = new UserRole();
                userRole.setUserId(bean.getId());
                userRole.setDataState("0");
                userRole.setRoleId(Long.valueOf(roleVoId));
                list.add(userRole);
            }
            userRoleMapper.batchInsert(list);
        }
    }

    @Override
    public void update(UserDto userDto) {
            User bean = BeanUtil.toBean(userDto, User.class);
        int update = userMapper.updateByPrimaryKeySelective(bean);
        if (update == 0) {
            throw new RuntimeException("更新失败");
        }
        //删除用户角色

        //添加用户角色
        if (userDto.getRoleVoIds() != null){
            userRoleMapper.deleteByUserId(bean.getId());
            List<UserRole> list=new ArrayList<>();
            for (String roleVoId : userDto.getRoleVoIds()) {
                UserRole userRole = new UserRole();
                userRole.setUserId(bean.getId());
                userRole.setDataState("0");
                userRole.setRoleId(Long.valueOf(roleVoId));
                list.add(userRole);
            }
            if (ObjectUtil.isNotEmpty(userDto.getRoleVoIds()))
                userRoleMapper.batchInsert(list);
        }
    }

    @Override
    public UserVo findById(Long userId) {

        UserVo user = userMapper.selectNowUser(userId);
        UserVo userVo = BeanUtil.toBean(user, UserVo.class);
        userVo.setPassword("******");
        return userVo;
    }

    @Override
    public void updateByStatus(Long id, String status) {
        userMapper.updateByStatus(id,status);
    }

    @Override
    public List<UserVo> list(UserDto userDto) {
        List<User> user=userMapper.selectAll(userDto);
        return BeanUtil.copyToList(user,UserVo.class);
    }

    @Override
    public void resetPassword(Long userId) {
        User user = new User();
        user.setId(userId);
        user.setPassword(BCrypt.hashpw(password,BCrypt.gensalt()));
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void delete(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        if(("0").equals(user.getDataState()))
            throw new BaseException(BasicEnum.USER_DELETE_ERROR);
        userMapper.delete(id);
    }


}
